package charts ;
import charts.series.Element;
import charts.series.bars.StackCollection;
import string.Utils;
import hxjson2.JSON;
import flash.geom.Point;
import charts.Base.KEYINFOSTRUCT;
import ColorConverter.Color;

class BarStack extends BarBase {
	
	public function new ( json: Dynamic, num: Float, group: Float ) {
		
		super(null, 0);
		
		this.style = {
			colors:				[Hex('#FF0000'),Hex('#00FF00')],
			values:				[],
			keys:				[],
			tip:				'#x_label# : #val#<br>Total: #total#',
			alpha:				0.6
		};
		
		ObjectHelper.merge_2( json, style );
		
		// this.axis = which_axis_am_i_attached_to(data, num);
		
		//
		// bars are grouped, so 3 bar sets on one chart
		// will arrange them selves next to each other
		// at each value of X, this.group tell the bar
		// where it is in that grouping
		//
		this.group = group;
		
		this.values = this.style.values;

		this.add_values();
	}
	
	//
	// return an array of key info objects:
	//
	public override function get_keys(): Array<KEYINFOSTRUCT> {		
		for (k in style.keys) { if (!Std.is(k, KEYINFOSTRUCT)) return []; } // TODO - check if this is correct
		return style.keys;
	}
	
	//
	// value is an array (a stack) of bar stacks
	//
	override function get_element( index: Int, value: Dynamic ): Element {
		
		//
		// this is the style for a stack:
		//
		var default_style: Dynamic = {
			tip:		this.style.tip,
			values:		value,
			colors:		this.style.colors,
			alpha:		this.style.alpha
		};
		
		return new StackCollection( index, default_style, this.group );
	}
	
	
	//
	// get all the Elements at this X position
	//
	override function get_all_at_this_x_pos( x: Float ):Array<Element> {
		
		var arr = new Array<Element>();
		var p:flash.geom.Point;
		var e:StackCollection;
		
		for ( i in 0...this.numChildren ) {
		
			// some of the children will will mask
			// Sprites, so filter those out:
			//
			if(Std.is(this.getChildAt(i),Element )) {
	
				e = cast(this.getChildAt(i),StackCollection);
			
				p = e.get_mid_point();
				if ( p.x == x ) {
					var children:Array<Element> = e.get_children();
					for ( child in children )
						arr.push( child );
				}
			}
		}
		
		return arr;
	}
}
